package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.utils.AliOssUtil;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.save(setmeal);
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if(setmealDishList != null && setmealDishList.size() > 0){
            setmealDishList.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });

            setmealDishMapper.save(setmealDishList);
        }
    }

    /**
     * 套餐信息分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐信息
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        // 起售中的套餐不能删除
        List<Setmeal> setmealList = setmealMapper.getByIds(ids);
        if(setmealList != null && setmealList.size() > 0){
            for(Setmeal setmeal : setmealList){
                if(setmeal.getStatus() == StatusConstant.ENABLE){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
                }
            }
        }

        // 删除套餐信息
        setmealMapper.deleteBatch(ids);

        // 删除套餐相关的套餐菜品信息
        setmealDishMapper.deleteBatch(ids);

        // 删除套餐对应的图片（阿里云oss）
        List<String> objectNames = new ArrayList<>();
        for(Setmeal setmeal : setmealList){
            String filePath = setmeal.getImage();
            // https://BucketName.Endpoint/ObjectName
            String objectName = filePath.substring(filePath.lastIndexOf("/") + 1);
            objectNames.add(objectName);
        }
        aliOssUtil.delete(objectNames);
    }


    /**
     * 更新套餐及套餐菜品信息
     * @param setmealDTO
     * @param isUpdate 1:更新菜品信息，0：起售停售菜品
     */
    @Transactional
    public void update(SetmealDTO setmealDTO, boolean isUpdate) {
        // 更新套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        if(isUpdate){
            Long setmealId = setmealDTO.getId();
            // 删除对应的套餐菜品信息
            setmealDishMapper.deleteBySetmealId(setmealId);

            // 插入对应的套餐菜品信息
            List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
            if(setmealDishList != null && setmealDishList.size() > 0){
                setmealDishList.forEach(setmealDish -> {
                    setmealDish.setSetmealId(setmealId);
                });
                setmealDishMapper.inertBatch(setmealDishList);
            }
        }
    }


    /**
     * 根据id获取套餐及套餐菜品信息
     * @param id
     * @return
     */
    public SetmealVO getById(Long id) {
        // 根据id获取套餐信息
        Setmeal setmeal = setmealMapper.getById(id);

        // 根据id获取套餐菜品信息
        List<SetmealDish> setmealDishList = setmealDishMapper.getBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);

        return setmealVO;
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
